package com.itcast.googleplay.fragment;

import java.util.List;

import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.itcast.googleplay.R;
import com.itcast.googleplay.adapter.MyBaseAdapter;
import com.itcast.googleplay.application.MyApplication;
import com.itcast.googleplay.domain.SubjectBean;
import com.itcast.googleplay.holder.BaseHolder;
import com.itcast.googleplay.holder.SubjectHolder;
import com.itcast.googleplay.protocol.SubjectProtocol;
import com.itcast.googleplay.ui.LoadingPage.ResultState;
import com.itcast.googleplay.utils.UIUtils;


public class SubjectFragment extends BaseFragment {

	private List<SubjectBean> subjectInfoList;
	private PullToRefreshListView prlv;
	private MyAdapter myAdapter;
	
	//javaee  10多年	oracle(NBA 金州勇士主场)	Apache  知识面,见识  网络头条(科技)
	//小米  笔记本
	//google 7.0 android N
	@Override
	public ResultState onSubLoad() {
		SubjectProtocol subjectProtocol = new SubjectProtocol();
		subjectInfoList = subjectProtocol.getData("subject", 0, "");
		if(subjectInfoList!=null){
			if(subjectInfoList.size()>0){
				return ResultState.STATE_SUCCESSED;
			}else{
				return ResultState.STATE_EMPTY;
			}
		}
		return ResultState.STATE_ERROR;
	}

	@Override
	public View onCreateSubSuccessedView() {
		prlv = (PullToRefreshListView) UIUtils.inflate(R.layout.layout_listview);
		ListView listView = prlv.getRefreshableView();
		
		myAdapter = new MyAdapter(subjectInfoList);
		listView.setAdapter(myAdapter);
		listView.setDivider(null);
		
		prlv.setMode(Mode.BOTH);
		prlv.setOnRefreshListener(new OnRefreshListener<ListView>() {

			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				if(prlv.getCurrentMode() == Mode.PULL_FROM_START){
					//刷新
					subjectInfoList.clear();
					//请求新的数据
					subShow();
					//数据适配器刷新
					myAdapter.notifyDataSetChanged();
					//隐藏刷新头
					prlv.onRefreshComplete();
				}else{
					//加载
					new Thread(){
						public void run() {
							SubjectProtocol subjectProtocol = new SubjectProtocol();
							List<SubjectBean> moreData = subjectProtocol.getData("subject", subjectInfoList.size(), "");
							if(moreData!=null && moreData.size()>0){
								subjectInfoList.addAll(moreData);
								UIUtils.runInMainThread(new Runnable() {
									@Override
									public void run() {
										//数据适配器刷新
										myAdapter.notifyDataSetChanged();
										prlv.onRefreshComplete();
									}
								});
							}else{
								UIUtils.runInMainThread(new Runnable() {
									@Override
									public void run() {
										//数据适配器刷新
										Toast.makeText(UIUtils.getContext(), "没有更多数据", 0).show();
										prlv.onRefreshComplete();
									}
								});
							}
						};
					}.start();
				}
			}
		});
		
		return prlv;
	}
	
	class MyAdapter extends MyBaseAdapter<SubjectBean>{

		public MyAdapter(List<SubjectBean> list) {
			super(list);
		}

		@Override
		public BaseHolder getSubBaseholder() {
			return new SubjectHolder();
		}
		
	}
}
